package com.diandian;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;



/**
 * @description JedisPoolManager
 * @author TDW
 * @date 2016年3月29日
 *
 */
public class JedisPoolManager {

	//方法静态化，构造私有化，clone重写，反正使用不恰当构造多的实例
	private JedisPoolManager() {

	};

	private static JedisPool pool;

	static {  
//        ResourceBundle bundle = ResourceBundle.getBundle("redis");  
//        if (bundle == null)   
//            throw new RuntimeException("properties file is not found");  
          
        JedisPoolConfig config = new JedisPoolConfig();  
//        config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));  //注意版本区别(pool2)：setMaxTotal 
        config.setMaxTotal(Integer.valueOf(100/*bundle.getString("redis.pool.maxActive")*/));  //注意版本区别(pool2)：setMaxTotal 
        config.setMaxIdle(Integer.valueOf(20/*bundle.getString("redis.pool.maxIdle")*/));  
//        config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));  //setMaxWaitMillis
        config.setMaxWaitMillis(Long.valueOf(1000/*bundle.getString("redis.pool.maxWait")*/));  //setMaxWaitMillis
//        config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));  
//        config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));  
          
        pool = new JedisPool(config, "112.74.94.149"/*bundle.getString("redis.ip")*/, Integer.valueOf(7200/*bundle.getString("redis.port")*/));  
    }  

	/**
	 * 从池里面获取Jedis实例，保证系统不会创建多的实例
	 * 
	 * @return
	 * @author
	 */
	public static Jedis createInstance() {
		Jedis jedis = pool.getResource();
		//jedis.auth("diandian");
		return jedis;
	}

	/**
	 * @param jedis
	 */
	public static void returnResource(Jedis jedis) {
		pool.returnResource(jedis);
	}
	
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return null;
	}

}
